const ble = require("../../utils/bluetooth");
const util = require("../../utils/util");
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    param_list : [
      { name: "充电过流保护", enable: false, address: 1, name1: "阈值_A", value1: "", address1: 1, name2: "延迟判定_ms", value2: "", address2: 1, name3: "释放阈值_S", value3: "", address3: 1},
      { name: "放电过流1保护", enable: false, address: 1, name1: "阈值_A", value1: "", address1: 1, name2: "延迟判定_ms", value2: "", address2: 1, name3: "释放阈值_S", value3: "", address3: 1},
      { name: "放电过流2保护", enable: false, address: 1, name1: "阈值_A", value1: "", address1: 1, name2: "延迟判定_ms", value2: "", address2: 1, name3: "释放阈值_S", value3: "", address3: 1},
    ],
    sencond_param_list : [
      { name: "二级充电过流保护", enable: false, address: 1, name1: "阈值_A", value1: "", address1: 1, name2: "延迟判定_ms", value2: "", address2: 1,
      name3: "释放模式", value3: "", address3: 1},
      { name: "二级放电过流1保护", enable: false, address: 1, name1: "阈值_A", value1: "", address1: 1, name2: "延迟判定_ms", value2: "", address2: 1},
      { name: "二级放电过流2保护", enable: false, address: -1, name1: "阈值_A", value1: "", address1: 1, name2: "延迟判定_ms", value2: "", address2: 1},
      { name: "短路保护", enable: false, address: -1, name1: "阈值_A", value1: "", address1: 1, name2: "延迟判定_us", value2: "", address2: 1}
    ],
    modify_list : []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (ble.isConncted()) {
      this.readData()
    }
  },

  async readData() {
    const {params, second_params} = await ble.getParamsWrapper(true)
    let filteredParams = params.filter(e => e.type == "electric");
    let secondfilteredParams = second_params.filter(e => e.type == "electric");

    console.log(secondfilteredParams)
    this.setData({
      param_list : filteredParams,
      sencond_param_list : secondfilteredParams
    })
  },

  changeValue1(e) {
    const index = e.currentTarget.dataset.index; 
    const value =  e.detail.value;
    this.data.param_list[index].value1 = value
    this.addModifyList({
      address : e.currentTarget.dataset.address,
      value : value * e.currentTarget.dataset.ratio
    })
  },

  changeValue2(e) {
    const index = e.currentTarget.dataset.index; 
    const value =  e.detail.value;
    this.data.param_list[index].value2 = value
    this.addModifyList({
      address : e.currentTarget.dataset.address,
      value : value * e.currentTarget.dataset.ratio
    })
  },

  changeValue3(e) {
    const index = e.currentTarget.dataset.index; 
    const value =  e.detail.value ;
    this.data.param_list[index].value3 = value
    this.addModifyList({
      address : e.currentTarget.dataset.address,
      value : value * e.currentTarget.dataset.ratio
    })
  },

  handleSwitchChange(e) {
    const index = e.currentTarget.dataset.index;  
    const checked = e.detail;
    this.data.param_list[index].enable = checked
    this.setData({
      param_list : this.data.param_list
    })
    this.addModifyList({
      address : e.currentTarget.dataset.address,
      value : checked ? 1 : 0
    })
  },

  handleSwitchChange2(e) {
    const index = e.currentTarget.dataset.index;  
    const checked = e.detail;
    this.data.sencond_param_list[index].enable = checked
    this.setData({
      sencond_param_list : this.data.sencond_param_list
    })
    this.addModifyList({
      address : e.currentTarget.dataset.address,
      value : checked ? 1 : 0
    })
  },
  
  changeValue12(e) {
    const index = e.currentTarget.dataset.index; 
    const value =  e.detail.value;
    this.data.sencond_param_list[index].value1 = value
    this.addModifyList({
      address : e.currentTarget.dataset.address,
      value : value * e.currentTarget.dataset.ratio
    })
  },

  changeValue22(e) {
    const index = e.currentTarget.dataset.index; 
    const value =  e.detail.value;
    this.data.sencond_param_list[index].value2 = value
    this.addModifyList({
      address : e.currentTarget.dataset.address,
      value : value * e.currentTarget.dataset.ratio
    })
  },

  changeValue32(e) {
    const index = e.currentTarget.dataset.index; 
    const value =  e.detail.value ;
    this.data.sencond_param_list[index].value3 = value
    this.addModifyList({
      address : e.currentTarget.dataset.address,
      value : value * e.currentTarget.dataset.ratio
    })
  },

  addModifyList(e) {
    var b_exists = false

    for (let i = 0; i < this.data.modify_list.length; i++) {
      const element = this.data.modify_list[i];
      if (element.address == e.address) {
        element.value = e.value
        b_exists = true
      }
    }

    if (!b_exists) {
      this.data.modify_list.push({
        address : e.address,
        value : e.value
      })
    }
  },

  async save() {
    if  (app.globalData.userInfo.current_permission < 1){
      wx.showModal({
        content: '您没有修改电流参数的权限，请前往验证一级权限密码',
      })

      return
    }

    console.log("电流开始修改啦")
    if (!ble.isConncted()) {
      return;
    }

    wx.showLoading({
      title: '保存中...',
    })
    console.log(this.data.modify_list)

    for (let i = 0; i < this.data.modify_list.length; i++) {
      const element = this.data.modify_list[i];
      let address = element.address
      let value = parseInt(element.value)
      console.log("发送地址：",address, value)
      await ble.writeValueWrapper(address, value)
      await util.sleep(500)
    }

    wx.hideLoading()
  }
})